﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace WFL.Windows.Forms
{
    public class ArcFormBase : FormBase
    {

        #region 圆角区域

        private Size _arcTopLeft;
        public Size ArcTopLeft
        {
            get { return _arcTopLeft; }
            set 
            {
                if (value.Width == 0
                    || value.Height == 0)
                {
                    value.Width = 0;
                    value.Height = 0;
                }
                if (_arcTopLeft != value)
                {
                    _arcTopLeft = value;
                }
            }
        }

        private Size _arcTopRight;
        public Size ArcTopRight
        {
            get { return _arcTopRight; }
            set
            {
                if (value.Width == 0
                    || value.Height == 0)
                {
                    value.Width = 0;
                    value.Height = 0;
                }
                if (_arcTopRight != value)
                {
                    _arcTopRight = value;
                }
            }
        }

        private Size _arcBottomLeft;
        public Size ArcBottomLeft
        {
            get { return _arcBottomLeft; }
            set
            {
                if (value.Width == 0
                    || value.Height == 0)
                {
                    value.Width = 0;
                    value.Height = 0;
                }
                if (_arcBottomLeft != value)
                {
                    _arcBottomLeft = value;
                }
            }
        }

        private Size _arcBottomRight;
        public Size ArcBottomRight
        {
            get { return _arcBottomRight; }
            set
            {
                if (value.Width == 0
                    || value.Height == 0)
                {
                    value.Width = 0;
                    value.Height = 0;
                }
                if (_arcBottomRight != value)
                {
                    _arcBottomRight = value;
                }
            }
        }

        #endregion


        #region 重写基类方法

        protected override Region GetWindowRegion()
        {
            bool flag = _arcTopLeft.IsEmpty
                && _arcBottomLeft.IsEmpty
                && _arcTopRight.IsEmpty
                && _arcBottomRight.IsEmpty;
            if (flag)
            {
                return null;
            }
            GraphicsPath path = new GraphicsPath();
            Rectangle bounds = new Rectangle(0, 0, Width, Height);
            //左上圆弧
            if (!_arcTopLeft.IsEmpty)
            {
                path.AddArc(bounds.Left, bounds.Top, _arcTopLeft.Width * 2, _arcTopLeft.Height * 2, 180, 90);
            }
            //上边直线
            path.AddLine(bounds.Left + _arcTopLeft.Width, bounds.Top, bounds.Right - _arcTopRight.Width, bounds.Top);
            //右上圆弧
            if (!_arcTopRight.IsEmpty)
            {
                path.AddArc(bounds.Right - _arcTopRight.Width * 2, bounds.Top, _arcTopRight.Width * 2, _arcTopRight.Height * 2, 270, 90);
            }
            //右边直线
            path.AddLine(bounds.Right, bounds.Top + _arcTopRight.Height, bounds.Right, bounds.Bottom - _arcBottomRight.Height);
            //右下圆弧
            if (!_arcBottomRight.IsEmpty)
            {
                path.AddArc(bounds.Right - _arcBottomRight.Width * 2, bounds.Bottom - _arcBottomRight.Height * 2, _arcBottomRight.Width * 2, _arcBottomRight.Height * 2, 0, 90);
            }
            //下边直线
            path.AddLine(bounds.Right - _arcBottomRight.Width, bounds.Bottom, bounds.Left + _arcBottomLeft.Width, bounds.Bottom);
            //左下圆弧
            if (!_arcBottomLeft.IsEmpty)
            {
                path.AddArc(bounds.Left, bounds.Bottom - _arcBottomLeft.Height * 2, _arcBottomLeft.Width * 2, _arcBottomLeft.Height * 2, 90, 90);
            }
            Region region = new Region(path);
            return region;
        }

        protected override int WmNcHitTest(Point point)
        {
            if (WindowState != FormWindowState.Maximized)
            {
                Size dragSize = SystemInformation.DragSize;
                if (point.Y >= Height - NcBottomHeight)
                {
                    if (point.X >= Width - Math.Max(dragSize.Width, _arcBottomRight.Width))
                    {
                        return Header.Winuser.HTBOTTOMRIGHT;
                    }
                    if (point.X <= Math.Max(dragSize.Width, _arcBottomLeft.Width))
                    {
                        return Header.Winuser.HTBOTTOMLEFT;
                    }
                }
                else if (point.Y <= NcTopBorderHeight)
                {
                    if (point.X >= Width - Math.Max(dragSize.Width, _arcTopRight.Width))
                    {
                        return Header.Winuser.HTTOPRIGHT;
                    }
                    if (point.X <= Math.Max(dragSize.Width, _arcTopLeft.Width))
                    {
                        return Header.Winuser.HTTOPLEFT;
                    }
                }
            }
            return base.WmNcHitTest(point);
        }

        #endregion


    }
}
